﻿using MB.Orm.DB;
using MB.RuleBase.Common;
using Model;
using Public.DataAccess;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.Service
{
    public class BDeliveryBox
    {

        public BDeliveryBox() { }


        /// <summary>
        /// 根据种子类型获取种子信息
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<LmSeed> GetlmSeedList(string seedType)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<LmSeed> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LmSeed>("DeliveryBox", "GetLmSeed", seedType);
                return lmSeedlist;
            }
        }

        /// <summary>
        /// 获取箱码类型
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<LookItem> GetDeliveryBoxType(string lookId)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<LookItem> lmSeedlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<LookItem>("DeliveryBox", "GetDeliveryBoxType", lookId);
                return lmSeedlist;
            }
        }

        /// <summary>
        /// 根据批次查询配送箱明细
        /// </summary>
        /// <param name="seedType"></param>
        /// <returns></returns>
        public List<LmDeliveryBoxBarCode> GetDeliveryBoxBarCodeList(int pageIndex, int pageSize, out int total, params object[] parValues)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<LmDeliveryBoxBarCode> lmDeliveryBoxBarCode = PagedDatabaseExcuteByXmlHelper.NewInstance.GetPagedObjectsByXml3<LmDeliveryBoxBarCode>("DeliveryBox", "GetDeliveryBoxBarCodeList", pageIndex, pageSize, out  total, parValues);
                return lmDeliveryBoxBarCode;
            }
        }

        /// <summary>
        /// 修改种子基数
        /// </summary>
        /// <param name="lmSeed"></param>
        /// <returns></returns>
        public int UpdateLmSeed(LmSeed lmSeed)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQueryByEntity<LmSeed>("DeliveryBox", "UpdateLmSeed", lmSeed);
        }


        public bool AddLmDeliveryBoxBarCodeInfo(List<LmDeliveryBoxBarCode> lmDeliveryBoxBarCodeList)
        {
            bool result = false;
            //定义事物
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            DbTransaction tran = null;
            try
            {
                // 打开数据连接事物
                cn.Open();
                tran = cn.BeginTransaction();
                if (lmDeliveryBoxBarCodeList == null || lmDeliveryBoxBarCodeList.Count <= 0) return false;

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(tran))
                {
                    bulk.WriteToServer("DeliveryBox", "InsertDeliveryBoxBarCode", lmDeliveryBoxBarCodeList);
                    result = true;
                }
                if (result)
                    tran.Commit();
                else
                    tran.Rollback();
                return result;
            }
            catch (Exception ex)
            {
                tran.Rollback();
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
        }
    }
}
